package forms;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import model.Database;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class RegisterSubmitForm extends ActionForm {
	private String username = null;
	private String password = null;
	private String passwordVerify = null;
	private String email = null;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordVerify() {
		return passwordVerify;
	}

	public void setPasswordVerify(String passwordVerify) {
		this.passwordVerify = passwordVerify;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if ((username == null) || (username.length() < 1) )
			errors.add("usernameMsgTagl", new ActionMessage("errors.required", "username"));
		
		if ((password == null) || (password.length() < 1))
			errors.add("passwordMsgTagl", new ActionMessage("errors.required", "password"));	
		
		if ((passwordVerify == null) || (passwordVerify.length() < 1))
			errors.add("passwordVerifyMsgTagl", new ActionMessage("errors.required", "passwordVerify"));
		
		if ((email == null) || (email.length() < 1)){
			errors.add("emailMsgTagl", new ActionMessage("errors.required", "email"));
			if(false){
				//email is already in db
				//you have already registered
			}
			
		}
		if(!password.equals(passwordVerify)){
			errors.add("passwordMsgTag1", new ActionMessage("errors.invalid", "Passwords do not match"));
			
		}
		try {
			if(!Database.isUniqueUsername(username)){
				errors.add("usernameMsgTag1", new ActionMessage("errors.invalid", "Username in use"));
			}
			
			if(!Database.isUniqueEmail(email)){
				errors.add("emailMsgTag1", new ActionMessage("errors.invalid", "Email Address Has already been registered"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return errors;
	}
}
